package com.park.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.park.entity.Consume;
import com.park.entity.Recharge;
import com.park.service.ConsumeService;
import com.park.service.RechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class MemberController {

    @Autowired
    private RechargeService rs;

    @Autowired
    private ConsumeService cs;

    @GetMapping("/member_recharge.html")
    public String memberRecharge() {
        return "member_recharge";
    }

    @GetMapping("/member_record.html")
    public String memberRecord() {
        return "member_record";
    }

    @GetMapping("/member/recharge/search")
    @ResponseBody
    public List getRechargesByMemberId(@RequestParam(value = "rechargeId", required = false) Integer rechargeId, @RequestParam("page") int page, @RequestParam("pageSize") int pageSize) {
        List<Object> ret = new ArrayList<>();
        Page<Recharge> parkPage = new Page<>();
        parkPage.setSize(pageSize);
        parkPage.setCurrent(page + 1);
        if (rechargeId == null) {
            List<Recharge> res = rs.list();
            ret.addAll(res.subList(page * pageSize, Math.min((page + 1) * pageSize, res.size())));
            ret.add(res.size() % pageSize == 0 ? res.size() / pageSize : res.size() / pageSize + 1);
            return ret;
        }
        ret.addAll(rs.getByRechargeId(parkPage, rechargeId).getRecords());
        ret.add(parkPage.getTotal() % pageSize == 0 ? parkPage.getTotal() / pageSize : parkPage.getTotal() / pageSize + 1);
        return ret;
    }

    @GetMapping("/member/record/search")
    @ResponseBody
    public List getConsumesByMemberId(@RequestParam(value = "memberId", required = false) Integer memberId, @RequestParam("page") int page, @RequestParam("pageSize") int pageSize) {
        List<Object> ret = new ArrayList<>();
        Page<Consume> parkPage = new Page<>();
        parkPage.setSize(pageSize);
        parkPage.setCurrent(page + 1);
        if (memberId == null) {
            List<Consume> res = cs.list();
            ret.addAll(res.subList(page * pageSize, Math.min((page + 1) * pageSize, res.size())));
            ret.add(res.size() % pageSize == 0 ? res.size() / pageSize : res.size() / pageSize + 1);
            return ret;
        }
        ret.addAll(cs.getByConsumeUserId(parkPage, memberId).getRecords());
        ret.add(parkPage.getTotal() % pageSize == 0 ? parkPage.getTotal() / pageSize : parkPage.getTotal() / pageSize + 1);
        return ret;
    }
}
